/*
** realloc.c for  in /home/alxevo/Documents/projets/malloc/malloc_norme
** 
** Made by Thibault Ravera
** Login   <ravera_t@epitech.net>
** 
** Started on  Sat Feb  4 16:38:54 2012 Thibault Ravera
** Last update Sun Feb  5 17:04:08 2012 Thibault Ravera
*/

#include	"malloc.h"

void		*create_new_chunk(void *ptr, size_t size)
{
  void		*new_chunk;

  new_chunk = malloc(size);
  if (new_chunk)
    {
      pthread_mutex_lock(&lock_list);
      memcpy(new_chunk, ptr, MIN(MEM2CHUNK(ptr)->size, size));
      pthread_mutex_unlock(&lock_list);
      free(ptr);
    }
  return (new_chunk);
}

void		*realloc(void *ptr, size_t size)
{
  if (ptr == NULL)
    return (malloc(size));
  if (size == 0)
    {
      free(ptr);
      return (NULL);
    }
  if ((MEM2CHUNK(ptr)->size & ~PINUSE) == MEMORY_REQUEST(NBLOCKS(size)))
    return (ptr);
  return (create_new_chunk(ptr, size));
}
